import pygame

from Actor.Lead_actor import Lead_Actor
from Game import SCREEN_WIDTH, SCREEN_HEIGHT, GAME_TITLE
from Scene.BaseScene import ScenePassStatus
from Scene.Welcome_scene import WelcomeScene
from Scene.tunnel_warfare_scene import TunnelWarScene
from Scene.Win_scene import WinScene
from Scene.Fail_scene import Fail_Scene

class TunnelWarfareGame:
    def __init__(self):
        pygame.init()
        # 设置显示区域
        pygame.display.set_caption(GAME_TITLE)
        self.surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        self.clock = pygame.time.Clock()
        self.yu_er = Lead_Actor(0, 0)
        self.current_scene = TunnelWarScene(self.yu_er)

    def run(self):
        while True:
            down_flag = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                if event.type == pygame.KEYDOWN:
                    down_flag = True
            key_list = pygame.key.get_pressed()
            # 1-2.获取场景视窗图像 并绘制窗口
            current_screen = self.current_scene.get_current_surface()
            self.surface.blit(current_screen, (0, 0))
            # 3. 场景的运动
            self.current_scene.run(down_flag, key_list)
            # 4. 切换场景
            pass_status, next_scene_name = self.current_scene.get_pass_status()
            if pass_status != ScenePassStatus.normal:
                if next_scene_name:
                    next_scene = globals()[next_scene_name](self.yu_er)
                    self.current_scene = next_scene
                else:
                    break
            self.clock.tick(20)
            pygame.display.flip()
